package com.example.demo.ela.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.ela.entiy.PartsEntiyEla;
import com.example.demo.ela.repository.PartsEntiyRepository;

@RestController
@RequestMapping(value = "/es/pasts")
public class EsPastsController {
	@Resource
	TransportClient client;
	
	@Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

	@Resource
	PartsEntiyRepository partsEntiyRepository;
	@RequestMapping(value = "/add")
	public PartsEntiyEla add(@RequestParam Long id,@RequestParam String name) {
        
		//创建类
		PartsEntiyEla detailOrderDocument =new PartsEntiyEla();
		
		detailOrderDocument.setId(id);
		
		detailOrderDocument.setName(name);
	
		
		PartsEntiyEla save = partsEntiyRepository.save(detailOrderDocument);
		
		return save;

	}
	
	
	@RequestMapping(value = "/query")
	public List<PartsEntiyEla> query(@RequestParam String name) {
        
		List<PartsEntiyEla> findByName = partsEntiyRepository.findByName(name);
		
		return findByName;

	}
	
	
	
	
	
	
	
	
	//IK分词结果
	@RequestMapping(value = "/queryik")
	public List<String> queryik(@RequestParam String name) {
		 AnalyzeRequestBuilder ikRequest = new AnalyzeRequestBuilder(elasticsearchTemplate.getClient(),

	                AnalyzeAction.INSTANCE,"lcfn",name);

	        ikRequest.setTokenizer("ik");

	        List<AnalyzeResponse.AnalyzeToken> ikTokenList = ikRequest.execute().actionGet().getTokens();

	        List<String> searchTermList = new ArrayList<>();

	        ikTokenList.forEach(ikToken -> { searchTermList.add(ikToken.getTerm()); });

		return searchTermList;

	}
	@RequestMapping(value = "/queryString")
	public SearchResponse queryString(@RequestParam String name) {
		 BoolQueryBuilder mustQuery = QueryBuilders.boolQuery();
		    mustQuery.must(QueryBuilders.matchAllQuery()); // 添加第1条must的条件 此处为匹配所有文档
		    
		    mustQuery.must(QueryBuilders.matchPhraseQuery("name", name));//添加第2条must的条件 title字段必须为【时间简史】
		       // ↑ 放入筛选条件(termQuery为精确搜索，大小写敏感且不支持*) 实验发现matchPhraseQuery可对中文精确匹配term
		    SearchRequestBuilder searchRequestBuilder = elasticsearchTemplate.getClient().prepareSearch("lcfn").setTypes("parts")
		    	           .setQuery(mustQuery);
		    
		 //   SearchResponse searchResponse = searchRequestBuilder.get();
		    
		    SearchResponse searchResponse = searchRequestBuilder.setFrom((1 - 1) * 200)//分页起始位置（跳过开始的n个）
		    	           .setSize(200)//本次返回的文档数量
		    		              .execute().actionGet();//执行搜索
		    	         
		  System.out.println("response="+searchResponse);
		return searchResponse;
		
	}
	

}
